package menus;

import beans.SessionManagedBean;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import lecture.Lecture;
import lecture.LectureManagedBean;
import user.Rights;
import user.Role;

/**
 *
 * @author Thorsten Ludwig
 */
@ManagedBean(name="menuWizardBean")
@RequestScoped
public class MenuWizardManagedBean {
    private boolean news;
    private boolean downloads;
    private boolean mails;
    private boolean registration;
    private boolean staticPages;
    private boolean taskSheets;
    private boolean handingIns;
    private boolean exerciseGroups;
    private boolean score;

    public MenuWizardManagedBean() {
    }

    public String createMenu() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        List<LectureMenuItem> items = lecture.getMenuItems();
        SessionManagedBean sb = c.getApplication().evaluateExpressionGet(c, "#{sessionBean}", SessionManagedBean.class);
        sb.setForwarded(true);
        MenuCategory admin = lecture.getCategorys().get(0);
        LectureMenuItem m;
        
        if (this.news) {
            m = items.get(9);
            Rights r = m.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            r.giveViewRights(Role.TUTOR);
            r.giveViewRights(Role.STUDENT);
            
            r.giveEditRights(Role.ADMIN);
            r.giveEditRights(Role.COADMIN);
            MenuCategory cat = new MenuCategory("Informationen");
            r = cat.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            r.giveViewRights(Role.TUTOR);
            r.giveViewRights(Role.STUDENT);
            r.giveViewRights(Role.GUEST);
            cat.addItem(m);
            lecture.addCategory(cat);
        }
        
        if (this.downloads) {
            m = items.get(4);
            Rights r = m.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            r.giveViewRights(Role.TUTOR);
            r.giveViewRights(Role.STUDENT);
            
            r.giveEditRights(Role.ADMIN);
            r.giveEditRights(Role.COADMIN);
            MenuCategory cat = new MenuCategory("Downloads");
            r = cat.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            r.giveViewRights(Role.TUTOR);
            r.giveViewRights(Role.STUDENT);
            cat.addItem(m);
            lecture.addCategory(cat);
        }
        
        if (this.mails) {
            m = items.get(10);
            Rights r = m.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            r.giveViewRights(Role.TUTOR);
            
            MenuCategory cat = new MenuCategory("Tutor");
            r = cat.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            r.giveViewRights(Role.TUTOR);
            
            cat.addItem(m);
            lecture.addCategory(cat);
            
            m = items.get(11);
            r = m.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            admin.addItem(m);
        }
        
        if (this.registration) {
            m = items.get(0);
            Rights r = m.getRights();
            r.giveViewRights(Role.GUEST);
            
            MenuCategory cat = new MenuCategory("Anmelden");
            cat.getRights().giveViewRights(Role.GUEST);
            cat.addItem(m);
            lecture.addCategory(cat);
        }
        
        if (this.staticPages) {
            m = items.get(8);
            Rights r = m.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            
            admin.addItem(m);
        }
        
        MenuCategory exercise = null;
        
        if (this.taskSheets) {
            m = items.get(12);
            Rights r = m.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            
            exercise = new MenuCategory("Übungen");
            r = exercise.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            r.giveViewRights(Role.TUTOR);
            r.giveViewRights(Role.STUDENT);
            exercise.addItem(m);
            
            m = items.get(13);
            r = m.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            r.giveViewRights(Role.TUTOR);
            r.giveViewRights(Role.STUDENT);
            exercise.addItem(m);
            
            lecture.addCategory(exercise);
            
        }
        
        //If this.handingIns is true, also taskSheets must be true
        if (this.handingIns){
            m = items.get(14);
            Rights r = m.getRights();
            r.giveViewRights(Role.STUDENT);
            
            exercise.addItem(m);
            
            m = items.get(15);
            r = m.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            r.giveViewRights(Role.TUTOR);
            exercise.addItem(m);
        }
        
        if (this.exerciseGroups){
            m = items.get(5);
            Rights r = m.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            
            exercise.addItem(m);
            
            m = items.get(6);
            r = m.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            
            exercise.addItem(m);
            
            m = items.get(7);
            r = m.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            r.giveViewRights(Role.TUTOR);
            r.giveViewRights(Role.STUDENT);
            exercise.addItem(m);
        }
        
        if (this.score) {
            m = items.get(16);
            Rights r = m.getRights();
            r.giveViewRights(Role.TUTOR);
            
            MenuCategory catScore = new MenuCategory("Punkte");
            r = catScore.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            r.giveViewRights(Role.TUTOR);
            r.giveViewRights(Role.STUDENT);
            catScore.addItem(m);
            
            lecture.addCategory(catScore);
            
            m = items.get(17);
            r = m.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            r.giveViewRights(Role.TUTOR);
            
            catScore.addItem(m);
            
            m = items.get(18);
            r = m.getRights();
            r.giveViewRights(Role.ADMIN);
            r.giveViewRights(Role.COADMIN);
            
            catScore.addItem(m);
            
            m = items.get(19);
            r = m.getRights();
            r.giveViewRights(Role.STUDENT);
            catScore.addItem(m);
        }
        
        
        m = items.get(20);
        m.getRights().removeViewRights(Role.ADMIN);
        
        m = items.get(3);
        m.getRights().giveViewRights(Role.ADMIN);
        m.getRights().giveViewRights(Role.COADMIN);
        admin.addItem(m);
        return "editMenu.jsf";
    }
    
    public boolean isDownloads() {
        return downloads;
    }

    public void setDownloads(boolean downloads) {
        this.downloads = downloads;
    }

    public boolean isExerciseGroups() {
        return exerciseGroups;
    }

    public void setExerciseGroups(boolean exerciseGroups) {
        this.exerciseGroups = exerciseGroups;
    }

    public boolean isHandingIns() {
        return handingIns;
    }

    public void setHandingIns(boolean handingIns) {
        this.handingIns = handingIns;
    }

    public boolean isMails() {
        return mails;
    }

    public void setMails(boolean mails) {
        this.mails = mails;
    }

    public boolean isNews() {
        return news;
    }

    public void setNews(boolean news) {
        this.news = news;
    }

    public boolean isRegistration() {
        return registration;
    }

    public void setRegistration(boolean registration) {
        this.registration = registration;
    }

    public boolean isScore() {
        return score;
    }

    public void setScore(boolean score) {
        this.score = score;
    }

    public boolean isStaticPages() {
        return staticPages;
    }

    public void setStaticPages(boolean staticPages) {
        this.staticPages = staticPages;
    }

    public boolean isTaskSheets() {
        return taskSheets;
    }

    public void setTaskSheets(boolean taskSheets) {
        this.taskSheets = taskSheets;
    }
}
